﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace MySafeLib
{
    public class SafeStringParser<T> 
    {
        public SafeStringParser()
        {
        }

        public bool Parse(string value, ref T result)
        {
            try
            {
                Type t = typeof(T);
                MethodInfo mi =  t.GetMethod("Parse",new Type[]{typeof(string)});
                result = (T) mi.Invoke(result,new object[]{value});
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show( MySafeLibResources.ArgumentNullException, MySafeLibResources.ParsingErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error );
                return false;
            }
            catch (FormatException)
            {
                MessageBox.Show( MySafeLibResources.FormatException, MySafeLibResources.ParsingErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error );
                return false;
            }
            catch (OverflowException)
            {
                MessageBox.Show( MySafeLibResources.OverflowException, MySafeLibResources.ParsingErrorString, MessageBoxButtons.OK, MessageBoxIcon.Error );
                return false;
            }
            catch(TargetException)
            {
                return false;
            }
            catch(ArgumentException)
            {
                return false;
            }
            catch(TargetInvocationException)
            {
                return false;
            }
            catch(TargetParameterCountException)
            {
                return false;
            }
            catch(MethodAccessException)
            {
                return false;
            }
            catch(InvalidOperationException)
            {
                return false;
            }
            catch(NotSupportedException)
            {
                return false;
            }
            return true;
        }
    }
}
